package net.dodoman.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import net.dodoman.annotation.MethodAnnotation.FontColor;

public class AnnotationMain {
	public static void main(String[] args) {
		AnnotationMain at = new AnnotationMain();
		at.showInfo();
	}

	@MethodAnnotation(fontColor = FontColor.GREEN)
	public void test(
			@ParamAnnotation(name = "testname", value = "testvalue") String str) {
		showInfo();
	}

	private void showInfo() {
		try {
			// 取得注解所在方法,从而得到所有注解
			Method tm = this.getClass().getMethod("test", String.class);
			Annotation[] methodAnnotations = tm.getAnnotations();
			Annotation[][] paramAnnotations = tm.getParameterAnnotations();
			for (Annotation annotation : methodAnnotations) {
				System.out.println("Tag is:" + annotation);
				if (MethodAnnotation.class.isAssignableFrom(annotation
						.annotationType())) {
					System.out.println("name="
							+ ((MethodAnnotation) annotation).name());
					System.out.println("value="
							+ ((MethodAnnotation) annotation).value());
				}
			}
			for (Annotation[] annotations : paramAnnotations) {
				for (Annotation annotation : annotations) {
					if (ParamAnnotation.class.isAssignableFrom(annotation
							.annotationType())) {
						System.out.println("Tag is:" + annotation);
						System.out.println("name="
								+ ((ParamAnnotation) annotation).name());
						System.out.println("value="
								+ ((ParamAnnotation) annotation).value());
					}
				}
			}
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
	}
}
